<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>客户管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
    <!--Excel导入-->
    <div style="margin-bottom: -15px;display: inline-block">
        <button type="button" class="layui-btn" id="upload" >
            <i class="layui-icon">&#xe67c;</i>导入数据
        </button>
        <a href="/custom/exportCustom">
            <button type="button" class="layui-btn" id="" style="margin-left: 0">
                <i class="layui-icon">&#xe67d;</i>导出Excel
            </button>
        </a>
    </div>

<!--列表显示-->
<table id="customTable" lay-filter="customFilter"></table>

<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

    <!--头部工具栏-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <!--<button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>-->
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <!--<button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>-->
    </div>
</script>

<!--部门添加form-->
<form id="customAddForm" class="layui-form" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">联系人</label>
        <div class="layui-input-block">
            <input type="text" name="contact" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="phone" placeholder="请输入" required  lay-verify="required|phone|number" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" placeholder="请输入" required  lay-verify="email" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">申请人</label>
        <div class="layui-input-block">
            <input type="text" name="applicant" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">代理人ID</label>
        <div class="layui-input-block">
            <input type="text" name="proxyId" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <input th:value="${session.userInfo.userName}" name="createBy" hidden>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="customAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--部门更新form-->
<form id="customUpdateForm" class="layui-form" lay-filter="customUpdateFilter" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">客户ID</label>
        <div class="layui-input-block">
            <input type="" name="customId" class="layui-input" readonly>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">联系人</label>
        <div class="layui-input-block">
            <input type="text" name="contact" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-block">
            <input type="text" name="phone" placeholder="请输入" required  lay-verify="required|phone|number" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" placeholder="请输入" required  lay-verify="email" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">申请人</label>
        <div class="layui-input-block">
            <input type="text" name="applicant" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">代理人ID</label>
        <div class="layui-input-block">
            <input type="text" name="proxyId" placeholder="请输入" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <input th:value="${session.userInfo.userName}" name="updateBy" hidden>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="customUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--js 代码-->
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table','layer','jquery','form','upload'], function(){
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        var form = layui.form;

        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            ,url: '/custom/importCustom' //上传接口
            ,auto:true // 选择文件后自动上传
            ,accept:"file"
            ,field:"file"
            ,done: function(res){
                //上传完毕回调
                if(res.code==0){
                    alert("导入成功");
                }
            }
            ,error: function(){
                //请求异常回调
            },
        });

        //部门列表js
        let customTable = table.render({
            elem: '#customTable'
            ,toolbar: '#toolbarHead'
            ,url: '/custom/queryAll' //数据接口
            ,page: true //开启分页
            ,cellMinWidth: 80
            ,cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'} //开启复选框
                    ,{field: 'customId', title: 'ID', sort: true, fixed: 'left'}
                    ,{field: 'contact', title: '联系人姓名', sort: true}
                    ,{field: 'phone', title: '手机号',  sort: true}
                    ,{field: 'email', title: '邮箱'}
                    ,{field: 'applicant', title: '申请人'}
                    ,{field: 'customAddress', title: '申请人地址',  sort: true}
                    ,{field: 'createBy', title: '创建者',sort: true}
                    ,{field: 'createTime', title: '创建时间'}
                    ,{field: 'updateBy', title: '更新者'}
                    ,{field: 'updateTime', title: '更新时间'}
                    ,{field: 'proxyId', title: '代理人ID'}
                    ,{field: 'remark', title: '备注'}
                    ,{fixed: 'right', align:'center', toolbar: '#barRowEdit'}
                ]
            ]
        });

        //行编辑工具栏监听事件
        table.on('tool(customFilter)', function(obj){
            //获取当前行数据
            var data = obj.data;
            //==不匹配类型  1=='1' true      ==== 匹配类型  1==='1' false
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/custom/deleteById",
                        type:"DELETE",
                        data:{customId:data.customId},
                        dataType:"json",
                        success:function (resp) {
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                //重新加载数据
                                customTable.reload();
                            }
                        },
                        error:function (e) {
                            layer.msg("请求失败");
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                form.val("customUpdateFilter",data);
                layer.open({
                    type: 1,
                    area:['500px', '450px'],
                    content: $('#customUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //头部工具栏监听事件
        table.on('toolbar(customFilter)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id);
            let  checkData = checkStatus.data;
            switch(obj.event){
                case 'export':
                    $.ajax({
                        url:"/custom/exportCustom",
                        type:"PUT",
                        data:data.field,
                        dataType:"json",
                        success:function (resp) {
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                //关闭弹框
                                layer.closeAll('page');
                                //重新加载数据
                                customTable.reload();
                            }
                        },
                        error:function (e) {
                            layer.msg("请求失败");
                        }
                    });
                    break;
                case 'add':
                    // layer.msg('添加');
                    //重置form表单
                    // $("#customAddForm").reset();
                    document.getElementById("customAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['500px', '400px'],
                        content: $('#customAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    //layer.msg('删除');
                    //layer.msg(JSON.stringify(checkData));
                    //判断是否选中行
                    if(checkData.length==0){
                        layer.msg('请至少选择一行数据');
                        return;
                    }
                    layer.confirm('真的删除行么', function(index) {
                        //获取所有选中行id
                        let customIds = "";
                        for (let i = 0; i < checkData.length; i++) {
                            customIds += checkData[i].customId + ",";
                        }
                        ;
                        customIds = customIds.substr(0, customIds.length - 1);
                        layer.msg(customIds);
                        //调用批量删除
                        $.ajax({
                            url: "/custom/deleteByIds",
                            type: "DELETE",
                            data: {customIds: customIds},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    customTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':

                    if(checkData.length!=1){
                        layer.msg('编辑只能选中一行');
                        return;
                    }
                    // alert(JSON.stringify(checkData));
                    form.val("customUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['400px', '250px'],
                        content: $('#customUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });

        //添加监听事件
        form.on('submit(customUpdateFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/custom/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        customTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });

        //更新监听事件
        form.on('submit(customAddFilter)', function(data){
            layer.msg(JSON.stringify(data.field));
            $.ajax({
                url:"/custom/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        customTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });

    });
</script>
</body>
</html>